
package org.search.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeListener;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import org.lobobrowser.html.HtmlRendererContext;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.parser.DocumentBuilderImpl;
import org.lobobrowser.html.test.SimpleHtmlRendererContext;
import org.serch.action.SearchAction;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class SearchFrame extends JFrame {

    JPanel pnPanel0;
    JLabel lbLabel1;
    JButton btBut0;
    JButton btBut1;
    JTextField tfText0;
    JPanel pnPanel1;
    HtmlPanel htmlPanel;

    public SearchFrame() {
        super("Search");
        setSize(500, 700);
        pnPanel0 = new JPanel();
        pnPanel0.setSize(1024, 700);
        GridBagLayout gbPanel0 = new GridBagLayout();
        GridBagConstraints gbcPanel0 = new GridBagConstraints();
        pnPanel0.setLayout(gbPanel0);

        lbLabel1 = new JLabel("Search Query");
        gbcPanel0.fill = GridBagConstraints.HORIZONTAL;
        gbcPanel0.gridx = 0;
        gbcPanel0.gridy = 0;
        gbcPanel0.insets = new Insets(10, 10, 0, 0);
        pnPanel0.add(lbLabel1, gbcPanel0);

        tfText0 = new JTextField();
        gbcPanel0.weightx = 0.5;
        gbcPanel0.gridx = 1;
        gbcPanel0.gridy = 0;
        gbcPanel0.insets = new Insets(10, 10, 0, 10);
        pnPanel0.add(tfText0, gbcPanel0);

        btBut1 = new JButton("Clear");
        gbcPanel0.weightx = 0.5;

        gbcPanel0.gridx = 0;
        gbcPanel0.gridy = 2;
        pnPanel0.add(btBut1, gbcPanel0);

        btBut0 = new JButton("Ok!");
        btBut0.setMnemonic(KeyEvent.VK_ENTER);
        gbcPanel0.weightx = 0.5;
        gbcPanel0.gridx = 1;
        gbcPanel0.gridy = 2;
        pnPanel0.add(btBut0, gbcPanel0);

        htmlPanel = new HtmlPanel();
        gbcPanel0.fill = GridBagConstraints.BOTH;
        gbcPanel0.ipady = 100; // make this component tall

        gbcPanel0.weighty = 0.60;

        gbcPanel0.weightx = 0.0;
        gbcPanel0.gridwidth = 3;
        gbcPanel0.gridx = 0;
        gbcPanel0.gridy = 3;
        pnPanel0.add(htmlPanel, gbcPanel0);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setContentPane(pnPanel0);
        // pack();
        setVisible(true);
        // setResizable(false);
        addListeners();
    }
    final String[] searchResult = new String[1];

    private void addListeners() {

        btBut0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                search();
            }
        });

        btBut1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                clearResult();
            }

        });
        
        btBut0.getActionMap().put(KeyEvent.VK_ENTER, new Action() {
            
            public void actionPerformed(ActionEvent arg0) {
                search();
            }
            
            public void setEnabled(boolean arg0) {
                
            }
            
            public void removePropertyChangeListener(PropertyChangeListener arg0) {
                
            }
            
            public void putValue(String arg0, Object arg1) {
                
            }
            
            public boolean isEnabled() {
                return true;
            }
            
            public Object getValue(String arg0) {
                return null;
            }
            
            public void addPropertyChangeListener(PropertyChangeListener arg0) {
                
            }
        });
        
        btBut0.addKeyListener(new KeyListener() {
            
            public void keyTyped(KeyEvent event) {
                if(event.getKeyCode() == KeyEvent.VK_ENTER){
                    search();
                }
            }
            
            public void keyReleased(KeyEvent event) {
                
            }
            
            public void keyPressed(KeyEvent event) {
                
            }
        });

    }

    private void renderResult() {
        try {
            HtmlRendererContext rcontext = new SimpleHtmlRendererContext(htmlPanel);
            Document doc = new DocumentBuilderImpl(rcontext)
                .parse(new ByteArrayInputStream(searchResult[0].getBytes()));

            htmlPanel.setDocument(doc, rcontext);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void clearResult() {
        htmlPanel.clearDocument();
        tfText0.setText(null);

    }

    private void search() {
        
        String query = tfText0.getText();
        if(query == null || query.trim().length() == 0){
            JOptionPane.showMessageDialog(tfText0, "Please enter some search query","Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        searchResult[0] = SearchAction.sendOutput(query);
        renderResult();
    }

}
